package experimentReport.servlet;

import jakarta.servlet.http.*;

import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.regex.*;

public class Six extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String s = req.getParameter("vertex");
        if (s == null || s.isEmpty()) {
            resp.sendRedirect("jsp/experimentReport/six.jsp");
            return;
        }
        BufferedImage img = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_BGR);
        Graphics2D g = img.createGraphics();
        g.fillRect(0, 0, 1000, 800);
        g.setColor(Color.BLUE);
        resp.setContentType("image/jpeg");
        Polygon pg = new Polygon();
        Pattern pt = Pattern.compile("(\\d+[,， ]+\\d+)");
        Matcher mt = pt.matcher(s);
        while (mt.find()) {
            String[] vertex = mt.group().split("[(),，]+");
            pg.addPoint(Integer.parseInt(vertex[0]), Integer.parseInt(vertex[1]));
        }
        g.draw(pg);
        ImageIO.write(img, "jpg", resp.getOutputStream());
    }
}
